/*******************************************************************************
 * Copyright (c) 2013 Prometheus Consulting
 *
 *     Licensed under the Apache License, Version 2.0 (the "License"); you may
 *     not use this file except in compliance with the License. You may obtain
 *     a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *     WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 *     License for the specific language governing permissions and limitations
 *     under the License.
 *******************************************************************************/
package nz.co.senanque.pizzaorder;

import java.util.List;

import nz.co.senanque.pizzaorder.instances.Customer;
import nz.co.senanque.vaadinsupport.FieldButtonPainter;
import nz.co.senanque.vaadinsupport.MaduraPropertyWrapper;
import nz.co.senanque.vaadinsupport.SimpleButtonPainter;
import nz.co.senanque.vaadinsupport.SubmitButtonPainter;
import nz.co.senanque.vaadinsupport.application.MaduraSessionManager;
import nz.co.senanque.vaadinsupport.viewmanager.HelpLayout;
import nz.co.senanque.vaadinsupport.viewmanager.ViewManaged;
import nz.co.senanque.vaadinsupport.viewmanager.ViewManager;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.vaadin.annotations.AutoGenerated;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;

/**
 * This shows how to set up a button that depends on a FieldButtonPainter
 * When the field named 'dynamic' is true the button becomes enabled.
 * The field is set by rules firing.
 * 
 * @author Roger Parkinson
 *
 */
public class CustomerLayout extends CustomComponent implements InitializingBean, ViewManaged {

	@AutoGenerated
	private AbsoluteLayout mainLayout;
	@AutoGenerated
	private TextField m_email;
	@AutoGenerated
	private HorizontalLayout horizontalLayout_1;
	@AutoGenerated
	private Button m_helpButton;
	@AutoGenerated
	private Button m_dynamicButton;
	@AutoGenerated
	private Button m_cancelButton;
	@AutoGenerated
	private Button m_saveButton;
	@AutoGenerated
	private Label label_1;
	@AutoGenerated
	private TextField m_address;
	@AutoGenerated
	private TextField m_name;
	private ViewManager m_viewManager;
    @Autowired private MaduraSessionManager m_maduraSessionManager;

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public CustomerLayout() {
		buildMainLayout();
		setCompositionRoot(mainLayout);		
	}
	
	public void afterPropertiesSet() throws Exception
	{
		

		// This section adds listeners to all the buttons and it
		// also registers them so that they can be monitored.
		// Registration also does good things with I18n.
		final MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
		final CustomComponent me = this;
		m_saveButton.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				getViewManager().switchScreen("nz.co.senanque.pizzaorder.MainLayout");
				getViewManager().getMainWindow().requestRepaint();
			}
		});
		maduraSessionManager.register(m_saveButton, new SubmitButtonPainter(m_maduraSessionManager));
		m_dynamicButton.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				getViewManager().popScreen();
				getViewManager().getMainWindow().requestRepaint();
			}
		});
		maduraSessionManager.register(m_dynamicButton, new FieldButtonPainter("dynamic","ADMIN",m_maduraSessionManager));
		m_cancelButton.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				getViewManager().popScreen();
				getViewManager().getMainWindow().requestRepaint();
			}
		});
		maduraSessionManager.register(m_cancelButton, new SimpleButtonPainter(m_maduraSessionManager));
		m_helpButton.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				getViewManager().pushScreen(HelpLayout.class.getName());
				getViewManager().getMainWindow().requestRepaint();
			}
		});
		maduraSessionManager.register(m_helpButton, new SimpleButtonPainter(m_maduraSessionManager));
		maduraSessionManager.register(m_name);
		maduraSessionManager.register(m_email);
		maduraSessionManager.register(m_address);
		maduraSessionManager.register(label_1);
	}
	
	/**
	 * We know that everything on this layout relates to the customer object
	 * so just pass the customer object and take it from there.
	 * @param customer
	 */
	public void bind(Customer customer)
	{
		MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
		bind(maduraSessionManager.getFieldList(customer));
	}
	/**
	 * Binding focuses this layout on the underlying data we want
	 * it to interact with. This allows it to be used (or re-used)
	 * @param properties
	 */
	private void bind(List<MaduraPropertyWrapper> properties)
	{
		MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
		maduraSessionManager.bind(m_saveButton,properties);
		maduraSessionManager.bind(m_dynamicButton,properties);
		maduraSessionManager.bind(m_cancelButton,properties);
		maduraSessionManager.bind(m_helpButton,properties);
		maduraSessionManager.bind(m_email,"email",properties);
		maduraSessionManager.bind(m_name,"name",properties);
		maduraSessionManager.bind(m_address,"address",properties);
	}
	/**
	 * This will remove the registration as this object is destroyed
	 * Cleanup of objects like this will limit the number of fields
	 * that have to be monitored and is efficient.
	 */
	public void destroy()
	{
		MaduraSessionManager maduraSessionManager = getMaduraSessionManager();
		maduraSessionManager.deregister(m_saveButton);
		maduraSessionManager.deregister(m_dynamicButton);
		maduraSessionManager.deregister(m_cancelButton);
		maduraSessionManager.deregister(m_helpButton);
		maduraSessionManager.deregister(m_name);
		maduraSessionManager.deregister(m_email);
		maduraSessionManager.deregister(m_address);
		maduraSessionManager.deregister(label_1); // not actually needed but nice for symmetry
	}

	@AutoGenerated
	private AbsoluteLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new AbsoluteLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// m_name
		m_name = new TextField();
		m_name.setImmediate(false);
		m_name.setWidth("-1px");
		m_name.setHeight("-1px");
		m_name.setSecret(false);
		mainLayout.addComponent(m_name, "top:60.0px;left:140.0px;");
		
		// m_address
		m_address = new TextField();
		m_address.setImmediate(false);
		m_address.setWidth("-1px");
		m_address.setHeight("-1px");
		m_address.setSecret(false);
		mainLayout.addComponent(m_address, "top:136.0px;left:140.0px;");
		
		// label_1
		label_1 = new Label();
		label_1.setImmediate(false);
		label_1.setWidth("-1px");
		label_1.setHeight("-1px");
		label_1.setValue("Customer");
		mainLayout.addComponent(label_1, "top:0.0px;left:169.0px;");
		
		// horizontalLayout_1
		horizontalLayout_1 = buildHorizontalLayout_1();
		mainLayout
				.addComponent(horizontalLayout_1, "top:260.0px;left:140.0px;");
		
		// m_email
		m_email = new TextField();
		m_email.setImmediate(false);
		m_email.setWidth("168px");
		m_email.setHeight("-1px");
		m_email.setSecret(false);
		mainLayout.addComponent(m_email, "top:96.0px;left:140.0px;");
		
		return mainLayout;
	}

	@AutoGenerated
	private HorizontalLayout buildHorizontalLayout_1() {
		// common part: create layout
		horizontalLayout_1 = new HorizontalLayout();
		horizontalLayout_1.setImmediate(false);
		horizontalLayout_1.setWidth("320px");
		horizontalLayout_1.setHeight("-1px");
		horizontalLayout_1.setMargin(false);
		
		// m_saveButton
		m_saveButton = new Button();
		m_saveButton.setCaption("Save");
		m_saveButton.setImmediate(true);
		m_saveButton.setWidth("-1px");
		m_saveButton.setHeight("-1px");
		horizontalLayout_1.addComponent(m_saveButton);
		
		// m_cancelButton
		m_cancelButton = new Button();
		m_cancelButton.setCaption("Cancel");
		m_cancelButton.setImmediate(true);
		m_cancelButton.setWidth("-1px");
		m_cancelButton.setHeight("-1px");
		horizontalLayout_1.addComponent(m_cancelButton);
		
		// m_dynamicButton
		m_dynamicButton = new Button();
		m_dynamicButton.setCaption("Dynamic");
		m_dynamicButton.setImmediate(true);
		m_dynamicButton.setWidth("-1px");
		m_dynamicButton.setHeight("-1px");
		horizontalLayout_1.addComponent(m_dynamicButton);
		
		// m_helpButton
		m_helpButton = new Button();
		m_helpButton.setCaption("Help");
		m_helpButton.setImmediate(true);
		m_helpButton.setWidth("-1px");
		m_helpButton.setHeight("-1px");
		horizontalLayout_1.addComponent(m_helpButton);
		horizontalLayout_1.setComponentAlignment(m_helpButton,
				new Alignment(48));
		
		return horizontalLayout_1;
	}

	public ViewManager getViewManager() {
		return m_viewManager;
	}

	public void setViewManager(ViewManager viewManager) {
		m_viewManager = viewManager;
	}
	public MaduraSessionManager getMaduraSessionManager() {
		return m_maduraSessionManager;
	}

	public void setMaduraSessionManager(MaduraSessionManager maduraSessionManager) {
		m_maduraSessionManager = maduraSessionManager;
	}
}
